/* Trabalho 1 de Sistemas Distribuidos
 * Autores: Danniel S. Dias
 * 			Daniel M. Mello
 * 			Bruno S. Iwami
 * 			Gabriel Garcia Pedrosa
 * Obs:Os argumentos passados para execucao sao :
 * server_root porta1 porta 2
 * porta1 e a porta2 sao as portas que receberao 2 clients*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <netdb.h>
#define MAX 1000
/*Estrutura que e passada para rodar na thread*/
typedef struct {
	int sockfd;
	int newsockfd;
	int n;
	int portno;
	int vet[MAX];
	int size;
} thread_info;
void error(const char *msg) {
	perror(msg);
	exit(1);
}
void sort(int v[], int n) {
	int i, aux, j;
	double t;
	struct timeval t1,t2;
	gettimeofday(&t1,NULL);
	for (i = 0; i < n; i++) {
		for (j = 0; j < n - 1; j++) {
			if (v[j] > v[j + 1]) {
				aux = v[j];
				v[j] = v[j + 1];
				v[j + 1] = aux;
			}
		}
	}
	gettimeofday(&t2,NULL);
	printf("\nTempo para ordenar = %ld segundos %ld milisegundos\n",t2.tv_sec - t1.tv_sec ,t2.tv_usec - t1.tv_usec);
}
/*Funcao passada na thread que abre o socket recebe do cliente um vetor*/
void*SocketHandler(void* lp) {
	thread_info *thr = (thread_info*) lp;
	//int sockfd, newsockfd, size;
	socklen_t clilen;
	struct sockaddr_in serv_addr, cli_addr;
	int buffer[MAX], i, aux = 0, size;
	struct timeval t1,t2;
	gettimeofday(&t1,NULL);
	/*Essa parte do codigo foi retirada do site http://www.linuxhowtos.org/C_C++/socket.htm
	 * e trata dos processos de abrir o socket e ler o que tem nele*/
	thr->sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (thr->sockfd < 0)
		error("ERROR opening socket");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(thr->portno);
	if (bind(thr->sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
			< 0)
		error("ERROR on binding");
	listen(thr->sockfd, 5);
	clilen = sizeof(cli_addr);
	thr->newsockfd
			= accept(thr->sockfd, (struct sockaddr *) &cli_addr, &clilen);
	if (thr->newsockfd < 0)
		error("ERROR on accept");
	bzero(buffer, sizeof(int) * MAX);
	thr->n = read(thr->newsockfd, buffer, sizeof(int) * MAX);
	if (thr->n < 0)
		error("ERROR reading from socket");
	size = thr->n / sizeof(int);
	for (i = 0; i < size; i++) {
		thr->vet[aux] = buffer[i];
		aux++;
	}
	/*****************************************************/
	gettimeofday(&t2,NULL);
	thr->size = size;

	return 0;
}

int main(int argc, char *argv[]) {

	int i, j, vet[MAX];
	pthread_t thread_id1 = 0, thread_id2 = 0;
	thread_info thr1, thr2;
	/**** Codigo para receber do cliente os vetores ordenalos******/
	thr1.portno = atoi(argv[1]);
	thr2.portno = atoi(argv[2]);
	pthread_create(&thread_id1, NULL, &SocketHandler, (void*) &thr1);
	pthread_create(&thread_id2, NULL, &SocketHandler, (void*) &thr2);

	pthread_join(thread_id1, NULL);
	pthread_join(thread_id2, NULL);

	for (i = 0; i < thr1.size; i++) {
		vet[i] = thr1.vet[i];
	}
	j = 0;
	for (i = thr1.size; i < thr2.size + thr1.size; i++) {
		vet[i] = thr2.vet[j];
		j++;
	}
	sort(vet, thr2.size + thr1.size);

	/****************************************************************/
	/****Codigo para devolver o vetor para o cliente*****************/
	thr1.n = write(thr1.newsockfd, vet, (sizeof(int)*thr2.size + sizeof(int)*thr1.size));
	thr2.n = write(thr2.newsockfd, vet, (sizeof(int)*thr2.size + sizeof(int)*thr1.size));
	/****************************************************************/
	close(thr1.sockfd);
	close(thr2.sockfd);

	return 0;
}
